<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next-haha.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next-haha.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next-haha.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ming.theyan.gs","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.25.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="缘起这个事情的需求，其实……就是闲得。 开个玩笑。我其实之前用 Tasker 这个 APP 做过类似的事情，见之前文章：分享一些自己DIY的task的profile，但是有一阵遇到了 android 手机的壁纸 bug，我的 pixel 给干挂了两回，其中有一回甚至给重制了才救回来，救回来之后就没起过自动更换壁纸的任务了。 这次是看到 V 站上有个大佬，展示自己极少的 APP 时，截图里展示了一个">
<meta property="og:type" content="article">
<meta property="og:title" content="利用 Termux 和 Termux-API 在 Android 手机上定时自动更新壁纸">
<meta property="og:url" content="https://ming.theyan.gs/2022/10/%E5%88%A9%E7%94%A8%20termux%20%E5%92%8C%20termux-api%20%E5%9C%A8%20android%20%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%AE%9A%E6%97%B6%E6%9B%B4%E6%96%B0%E5%A3%81%E7%BA%B8/index.html">
<meta property="og:site_name" content="运维烂笔头">
<meta property="og:description" content="缘起这个事情的需求，其实……就是闲得。 开个玩笑。我其实之前用 Tasker 这个 APP 做过类似的事情，见之前文章：分享一些自己DIY的task的profile，但是有一阵遇到了 android 手机的壁纸 bug，我的 pixel 给干挂了两回，其中有一回甚至给重制了才救回来，救回来之后就没起过自动更换壁纸的任务了。 这次是看到 V 站上有个大佬，展示自己极少的 APP 时，截图里展示了一个">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/haw-haw/image-hosting/master/img/WechatIMG116.jpeg">
<meta property="article:published_time" content="2022-10-13T14:37:22.000Z">
<meta property="article:modified_time" content="2022-10-18T12:12:35.000Z">
<meta property="article:author" content="老杨">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Termux">
<meta property="article:tag" content="Termux-API">
<meta property="article:tag" content="ImageMagick">
<meta property="article:tag" content="termux-job-scheduler">
<meta property="article:tag" content="termux-wallpaper">
<meta property="article:tag" content="F-Dorid">
<meta property="article:tag" content="convert">
<meta property="article:tag" content="magick">
<meta property="article:tag" content="cronie">
<meta property="article:tag" content="apt">
<meta property="article:tag" content="pkg">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/haw-haw/image-hosting/master/img/WechatIMG116.jpeg">


<link rel="canonical" href="https://ming.theyan.gs/2022/10/%E5%88%A9%E7%94%A8%20termux%20%E5%92%8C%20termux-api%20%E5%9C%A8%20android%20%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%AE%9A%E6%97%B6%E6%9B%B4%E6%96%B0%E5%A3%81%E7%BA%B8/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://ming.theyan.gs/2022/10/%E5%88%A9%E7%94%A8%20termux%20%E5%92%8C%20termux-api%20%E5%9C%A8%20android%20%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%AE%9A%E6%97%B6%E6%9B%B4%E6%96%B0%E5%A3%81%E7%BA%B8/index.html","path":"2022/10/利用 termux 和 termux-api 在 android 手机上定时更新壁纸/index.html","title":"利用 Termux 和 Termux-API 在 Android 手机上定时自动更新壁纸"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>利用 Termux 和 Termux-API 在 Android 手机上定时自动更新壁纸 | 运维烂笔头</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-106574959-2"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-106574959-2","only_pageview":false,"measure_protocol_api_secret":null}</script>
  <script src="/js/third-party/analytics/google-analytics.js" defer></script>

  <script src="/js/third-party/analytics/baidu-analytics.js" defer></script>
  <script async src="https://hm.baidu.com/hm.js?fdef9ded31bdb8b2dab08eddebdd5fed"></script>







  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script><script src="/js/bookmark.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  




<!-- google adsense -->
<script data-ad-client="ca-pub-1045025618858716" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="运维烂笔头" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">运维烂笔头</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">一个 SA 老兵的工作日志</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-projects"><a href="/projects" rel="section"><i class="fa fa-code fa-fw"></i>projects</a></li><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%98%E8%B5%B7"><span class="nav-number">1.</span> <span class="nav-text">缘起</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="nav-number">2.</span> <span class="nav-text">环境准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Termux-%E5%92%8C-Termux-API"><span class="nav-number">2.1.</span> <span class="nav-text">Termux 和 Termux-API</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ImageMagick-%E5%8F%8A%E5%85%B6%E4%BB%96%E8%BD%AF%E4%BB%B6"><span class="nav-number">2.2.</span> <span class="nav-text">ImageMagick 及其他软件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.</span> <span class="nav-text">具体实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%93%E5%8F%96%E6%AF%92%E9%B8%A1%E6%B1%A4"><span class="nav-number">3.1.</span> <span class="nav-text">抓取毒鸡汤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9A%E6%97%B6%E6%8A%93%E5%8F%96%E5%A3%81%E7%BA%B8%E5%B9%B6%E5%A4%84%E7%90%86%EF%BC%88%E7%94%A8-ImageMagick%EF%BC%89"><span class="nav-number">3.2.</span> <span class="nav-text">定时抓取壁纸并处理（用 ImageMagick）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E5%A3%81%E7%BA%B8"><span class="nav-number">3.3.</span> <span class="nav-text">设置壁纸</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%8A%E6%95%B4%E4%B8%AA%E5%B7%A5%E4%BD%9C%E8%87%AA%E5%8A%A8%E5%8C%96"><span class="nav-number">3.4.</span> <span class="nav-text">把整个工作自动化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%A0%E4%B8%AA%E5%9D%91"><span class="nav-number">4.</span> <span class="nav-text">几个坑</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%AD%E6%96%87%E5%AD%97%E4%BD%93"><span class="nav-number">4.1.</span> <span class="nav-text">中文字体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#crond-vs-termux-job-scheduler"><span class="nav-number">4.2.</span> <span class="nav-text">crond vs termux-job-scheduler</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ImageMagick-%E7%9A%84-crop-%E8%B7%9F-gravity-Center-%E4%B8%80%E8%B5%B7%E7%94%A8%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">4.3.</span> <span class="nav-text">ImageMagick 的 -crop 跟 -gravity Center 一起用的问题</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#change-wallpaper-sh-%E4%BB%A3%E7%A0%81"><span class="nav-number">5.</span> <span class="nav-text">change_wallpaper.sh 代码</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">老杨</p>
  <div class="site-description" itemprop="description">好记性比不过烂笔头</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">114</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">509</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/haw-haw" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;haw-haw" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:blog@theyan.gs" title="E-Mail → mailto:blog@theyan.gs" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/1494877243" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;1494877243" rel="noopener me" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://twitter.com/6fool" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;6fool" rel="noopener me" target="_blank"><i class="fab fa-twitter fa-fw"></i>Twitter</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://bad-pencil.github.io/" title="https:&#x2F;&#x2F;bad-pencil.github.io" rel="noopener" target="_blank">github 镜像站</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://hawhaw.gitee.io/" title="https:&#x2F;&#x2F;hawhaw.gitee.io" rel="noopener" target="_blank">gitee 镜像站</a>
            </li>
        </ul>
      </div>
    </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ming.theyan.gs/2022/10/%E5%88%A9%E7%94%A8%20termux%20%E5%92%8C%20termux-api%20%E5%9C%A8%20android%20%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%AE%9A%E6%97%B6%E6%9B%B4%E6%96%B0%E5%A3%81%E7%BA%B8/index.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="老杨">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="运维烂笔头">
      <meta itemprop="description" content="好记性比不过烂笔头">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="利用 Termux 和 Termux-API 在 Android 手机上定时自动更新壁纸 | 运维烂笔头">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          利用 Termux 和 Termux-API 在 Android 手机上定时自动更新壁纸
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-10-13 22:37:22" itemprop="dateCreated datePublished" datetime="2022-10-13T22:37:22+08:00">2022-10-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-10-18 20:12:35" itemprop="dateModified" datetime="2022-10-18T20:12:35+08:00">2022-10-18</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="缘起"><a href="#缘起" class="headerlink" title="缘起"></a>缘起</h2><p>这个事情的需求，其实……就是闲得。</p>
<p>开个玩笑。我其实之前用 Tasker 这个 APP 做过类似的事情，见之前文章：<a href="/2018/07/%E5%88%86%E4%BA%AB%E4%B8%80%E4%BA%9B%E8%87%AA%E5%B7%B1DIY%E7%9A%84task%E7%9A%84profile/index.html">分享一些自己DIY的task的profile</a>，但是有一阵遇到了 android 手机的壁纸 bug，我的 pixel 给干挂了两回，其中有一回甚至给重制了才救回来，救回来之后就没起过自动更换壁纸的任务了。</p>
<p>这次是看到 V 站上有个大佬，展示自己极少的 APP 时，截图里展示了一个桌面，上面的壁纸上有格言，还有英文单词。于是我就自然而然的想要不要把毒鸡汤也写到壁纸上呢？：）</p>
<span id="more"></span>

<h2 id="环境准备"><a href="#环境准备" class="headerlink" title="环境准备"></a>环境准备</h2><h3 id="Termux-和-Termux-API"><a href="#Termux-和-Termux-API" class="headerlink" title="Termux 和 Termux-API"></a>Termux 和 Termux-API</h3><p>我这里环境（Termux 和 Termux-API）是现成的，但如果没有需要安装的话，请记住，不要装 google play 上的 Termux 和 Termux-API，而是要装 F-Droid 这个市场上的 Termux 和 Termux-API！</p>
<p>注意：要给 Termux 足够的权限（也许 Termux-API 也需要）：</p>
<ul>
<li>后台运行的权限（不要被节电模式给干掉）</li>
<li>要设置为随机启动</li>
</ul>
<p>最后，还要打开 Termux，输入：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt install termux-api;</span><br><span class="line"><span class="comment"># pkg install termux-api; # 也可以用命令 pkg</span></span><br></pre></td></tr></table></figure>

<h3 id="ImageMagick-及其他软件"><a href="#ImageMagick-及其他软件" class="headerlink" title="ImageMagick 及其他软件"></a>ImageMagick 及其他软件</h3><p>打开 Termux，继续输入：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt install ImageMagick;</span><br><span class="line">apt install wget curl grep sed;</span><br></pre></td></tr></table></figure>

<h2 id="具体实现"><a href="#具体实现" class="headerlink" title="具体实现"></a>具体实现</h2><h3 id="抓取毒鸡汤"><a href="#抓取毒鸡汤" class="headerlink" title="抓取毒鸡汤"></a>抓取毒鸡汤</h3><p><a target="_blank" rel="noopener" href="https://djt.theyan.gs/">毒鸡汤</a> 也是我自己 fork 了某个大佬的代码，自己搭建的服务。本身没提供 API 服务，这里也没想再开发一个 API 接口，所以直接模拟 web 访问，然后把需要的数据抓出来即可。不过我们还需要手工折行。</p>
<h3 id="定时抓取壁纸并处理（用-ImageMagick）"><a href="#定时抓取壁纸并处理（用-ImageMagick）" class="headerlink" title="定时抓取壁纸并处理（用 ImageMagick）"></a>定时抓取壁纸并处理（用 ImageMagick）</h3><p>我自己有个壁纸服务：<a target="_blank" rel="noopener" href="https://wallpaper.theyan.gs/wp">壁纸</a>，会定时更新输出的壁纸，后台爬虫是用 go 实现的。</p>
<p>这里我就直接用了自己的这个壁纸服务，定时（每小时）去抓一个壁纸回来。接着做如下处理：</p>
<ul>
<li>如果宽小于 1080 或者高小于 2400 的话，会首先被按原比例放大，直到宽达到 1080 或高达到 2400（具体看哪种放大的比率小）。</li>
<li>再接着在壁纸正中截取一个 1080x2400 的图片出来</li>
<li>最后在这个截取出来的 1080x2400 的图片的合适位置写上毒鸡汤服务中抓取出来的文本数据，把最终结果保存为文件</li>
</ul>
<h3 id="设置壁纸"><a href="#设置壁纸" class="headerlink" title="设置壁纸"></a>设置壁纸</h3><p>最后自然是把上一步保留的文件用 termux-wallpaper 命令（来自于软件包 Termux-API）设置成新壁纸。</p>
<h3 id="把整个工作自动化"><a href="#把整个工作自动化" class="headerlink" title="把整个工作自动化"></a>把整个工作自动化</h3><p>把上面的流程写成代码（假设存为文件 $HOME&#x2F;bin&#x2F;change_wallpaper.sh），再用 termux-job-scheduler（来自于软件包 Termux-API）将这个代码设置为定时运行。我的例子里我是这样用的。打开 Termux，输入命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">termux-job-scheduler \</span><br><span class="line">    --persisted <span class="literal">true</span> \</span><br><span class="line">    --period-ms 3600000 \</span><br><span class="line">    -s <span class="variable">$HOME</span>/bin/change_wallpaper.sh;</span><br><span class="line"><span class="comment"># persisted 是指重启后保持有效</span></span><br><span class="line"><span class="comment"># period-ms 3600000 是指一小时跑一次</span></span><br></pre></td></tr></table></figure>

<h2 id="几个坑"><a href="#几个坑" class="headerlink" title="几个坑"></a>几个坑</h2><h3 id="中文字体"><a href="#中文字体" class="headerlink" title="中文字体"></a>中文字体</h3><p>在图片上写中文（含全角标点）时需要用中文的字体，因为普通字体里没有中文字符呀！</p>
<p>所以，要在所有可用字体里选出有中文字符且全角标点位置不在中间的，其实还有个想法，就是想找一个酷一点的中文字体，我把所有可用字体做了个循环，测试写中文字符以及全角标点，结果保存到文件里，代码大概如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> $(\</span><br><span class="line">    magick -list font | \</span><br><span class="line">    grep <span class="string">&quot;Font:&quot;</span> | \</span><br><span class="line">    awk <span class="string">&#x27;&#123;print $2&#125;&#x27;</span> \</span><br><span class="line">)</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">    convert \</span><br><span class="line">        -font <span class="variable">$i</span> \</span><br><span class="line">        -pointsize 72 \</span><br><span class="line">        label:测，。试<span class="built_in">test</span> \</span><br><span class="line">        x.jpg x-<span class="variable">$&#123;i&#125;</span>.jpg</span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<p>我先在我的 Mac 里跑的，找到一堆合适的字体以后，再去找 Termux 里 <code>magick -list font | grep &quot;Fonts:&quot; | awk &#39;&#123;print $2&#125;&#39;</code> 的结果来做对比，结果发现，匹配上的只有一种字体：<strong>Noto-Serif-CJK-SC</strong>。:(</p>
<p>当然，也可以再单独在 Termux 里安装新的好看的可用字体。但我没这么做，看以后需求吧。</p>
<h3 id="crond-vs-termux-job-scheduler"><a href="#crond-vs-termux-job-scheduler" class="headerlink" title="crond vs termux-job-scheduler"></a>crond vs termux-job-scheduler</h3><p>其实最早这个方案我是打算用 crond 来做定时任务的，软件都安装设置好了。</p>
<p>打开 Termux，</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt install cronie termux-services;</span><br></pre></td></tr></table></figure>

<p>关闭并重新打开 Termux</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sv-enable crond;</span><br></pre></td></tr></table></figure>

<p>但是发现用 crond 跑其他任务都没问题，但是跑 termux-API 里的命令总会报 Selinux 的错误！实在是搞不定啊，于是才转向 termux-job-scheduler 方案。</p>
<p>其实刚用 termux-job-scheduler 时日志里也出跟 crond 一样的信息，当时心就哇凉了，但没想到后来自己居然就好了？！：）</p>
<h3 id="ImageMagick-的-crop-跟-gravity-Center-一起用的问题"><a href="#ImageMagick-的-crop-跟-gravity-Center-一起用的问题" class="headerlink" title="ImageMagick 的 -crop 跟 -gravity Center 一起用的问题"></a>ImageMagick 的 -crop 跟 -gravity Center 一起用的问题</h3><p>ImageMagick 软件包里的命令：<strong>convert</strong> 有个参数：*-crop*，功能是用来从图片上截取一部分，这个参数还需要提供 x、y 两个参数，这两个可以理解为横坐标（x）和纵坐标（y），这个时候学过一点点编程基础的小可爱们是不是自然而然都会以为 <em>-crop</em> 参数（其实是操作参数，可以理解为命令）截取的长方形图片是以这个横坐标 x、纵坐标y 为顶点的呢？</p>
<p>我一开始也是这么认为的，所以当我用 <em>-gravity Center</em> 将坐标零点设置为图片中心之后，我将参数 x 和 y 分别设置成了 -540 和 -1200，结果就悲催了，最后经过调试，才知道当有 <em>-gravity Center</em> 时，x 和 y 是指截取长方形的中心的坐标！！！！</p>
<p>所以，这里的 x 和 y 都应该是 0 才对！！</p>
<h2 id="change-wallpaper-sh-代码"><a href="#change-wallpaper-sh-代码" class="headerlink" title="change_wallpaper.sh 代码"></a>change_wallpaper.sh 代码</h2><p>最后，show you the code:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/data/data/com.termux/files/usr/bin/bash</span></span><br><span class="line"></span><br><span class="line">TMP_IMG=<span class="string">&quot;<span class="subst">$(mktemp $HOME/tmp/tmpimg.XXXXXX)</span>&quot;</span></span><br><span class="line">sublen=8   <span class="comment"># 每行显示的字符个数</span></span><br><span class="line">DJT_TXT=<span class="string">&quot;<span class="subst">$(</span></span></span><br><span class="line"><span class="subst"><span class="string">        curl -s https://djt.theyan.gs | \</span></span></span><br><span class="line"><span class="subst"><span class="string">        grep -Po &#x27;(xxxxxxxxxx)</span>&#x27; | \</span></span><br><span class="line"><span class="string">        sed -r &#x27;s/.&#123;&#x27;&quot;</span><span class="variable">$sublen</span><span class="string">&quot;&#x27;&#125;/&amp;\n/g&#x27; \</span></span><br><span class="line"><span class="string">)&quot;</span></span><br><span class="line"></span><br><span class="line">wget -q -O - https://wallpaper.theyan.gs/wp | \</span><br><span class="line">    convert - \</span><br><span class="line">        -resize <span class="string">&#x27;1080x2400^&lt;&#x27;</span> \</span><br><span class="line">        - | \</span><br><span class="line">    convert - -gravity Center \</span><br><span class="line">        -crop 1080x2400+0+0 \</span><br><span class="line">        +repage \</span><br><span class="line">        - | \</span><br><span class="line">    magick - -font Noto-Serif-CJK-SC \</span><br><span class="line">        -pointsize 128 \</span><br><span class="line">        -fill DarkViolet \</span><br><span class="line">        -annotate +5+500 \</span><br><span class="line">        <span class="string">&quot;<span class="variable">$DJT_TXT</span>&quot;</span> \</span><br><span class="line">        <span class="variable">$TMP_IMG</span></span><br><span class="line"></span><br><span class="line">termux-wallpaper -f <span class="variable">$TMP_IMG</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">rm</span> <span class="variable">$TMP_IMG</span></span><br><span class="line"><span class="comment"># 抓毒鸡汤那一段 grep 的正则需要自己调整这个程序才能正常跑</span></span><br><span class="line"><span class="comment"># 不要来抓我的毒鸡汤的数据呀：（，这个项目程序、数据都是开源的，可以直接下载的，地址毒鸡汤上有。</span></span><br></pre></td></tr></table></figure>

<p>最后，附壁纸截图一张：</p>
<p><img src="https://raw.githubusercontent.com/haw-haw/image-hosting/master/img/WechatIMG116.jpeg" alt="手机壁纸截图"></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechat-reward.png" alt="老杨 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay-reward.png" alt="老杨 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://twitter.com/6fool">
            <span class="icon">
              <i class="fab fa-twitter"></i>
            </span>

            <span class="label">Twitter</span>
          </a>
      </div>

      <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/Termux/" rel="tag"># Termux</a>
              <a href="/tags/Termux-API/" rel="tag"># Termux-API</a>
              <a href="/tags/ImageMagick/" rel="tag"># ImageMagick</a>
              <a href="/tags/termux-job-scheduler/" rel="tag"># termux-job-scheduler</a>
              <a href="/tags/termux-wallpaper/" rel="tag"># termux-wallpaper</a>
              <a href="/tags/F-Dorid/" rel="tag"># F-Dorid</a>
              <a href="/tags/convert/" rel="tag"># convert</a>
              <a href="/tags/magick/" rel="tag"># magick</a>
              <a href="/tags/cronie/" rel="tag"># cronie</a>
              <a href="/tags/apt/" rel="tag"># apt</a>
              <a href="/tags/pkg/" rel="tag"># pkg</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/09/Best%20Practices%20for%20VPC%20IP%20Address%20Allocation/index.html" rel="prev" title="Best Practices for VPC IP Address Allocation">
                  <i class="fa fa-angle-left"></i> Best Practices for VPC IP Address Allocation
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/11/How%20to%20Install%20Xubuntu%20on%20a%20Chromebook/index.html" rel="next" title="How to Install Xubuntu on a Chromebook">
                  How to Install Xubuntu on a Chromebook <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">老杨</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
